package com.example.bookmanager.Controller;

import com.example.bookmanager.Enum.Enums;
import com.example.bookmanager.Model.BookInfo;
import com.example.bookmanager.Model.PageResult;
import com.example.bookmanager.Model.RequestPage;
import com.example.bookmanager.Model.Result;
import com.example.bookmanager.Service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.awt.print.Book;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo book) {
        log.info("addBook");
        if(!StringUtils.hasLength(book.getAuthor())||!StringUtils.hasLength(book.getBookName())) {
            return false;
        }
     Integer result = bookService.addBook(book);
        if(result == 0){
            return false;
        }
        return true;
    }
    @RequestMapping("/getListByPage")
    public Result getBookList(PageResult pageResult, HttpSession session) {
        log.info("getBookList");
        PageResult<BookInfo> result = bookService.getBookList(pageResult);
        return Result.success(result);
    }

    @RequestMapping("/getBook")
    public BookInfo getBook(Integer bookId) {
        BookInfo bookInfo = bookService.getBook(bookId);
        return bookInfo;
    }

    @RequestMapping("/updateBook")
   public boolean updataBook(BookInfo book) {
        log.info("updataBook");
        if(book.getId() == null) {
            return false;
        }
        if(!StringUtils.hasLength(book.getBookName())||"".equals(book.getBookName().trim())) {
            return false;
        }
        Integer result = bookService.updateBook(book);
        try {
            if(result > 0){
                return true;
            }
        }catch (Exception e) {
            log.error("updateBook error, e: {}", e);
            return false;
        }
return true;
   }

   @RequestMapping("/deleteBook")
   public boolean deleteBook(Integer bookId) {
       try {
           BookInfo bookInfo = new BookInfo();
           bookInfo.setId(bookId);
           bookInfo.setStatus(Enums.DELETE.getCode());
           Integer result = bookService.updateBook(bookInfo);
           if(result > 0){
               return true;
           }
           return false;
       }catch (Exception e) {
           return false;
       }

   }

   @RequestMapping("/batchDelete")
   public boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("batchDelete");
        if(ids == null || ids.size() == 0) {
            return true;
        }
        try {
            Integer result = bookService.batchDelete(ids);
            if(result > 0){
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("batchDelete error, e: {}", e);
            return false;
        }
   }

}
